package com.gaojx.lang.rule;

import com.gaojx.lang.rule.impl.RuleServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author junxiang.gao
 * @since 2020/8/19
 */
public enum RuleFactory {

    /**
     * 单例
     */
    INSTANCE;

    private static final Logger LOGGER = LoggerFactory.getLogger(RuleServiceImpl.class);

    public List<IRule> getRuleList() throws Exception {
        //调用普通的JAVA实现做对比
        List<IRule> rules = new ArrayList<>();
        NormalJavaRule normalJavaRule = new NormalJavaRule();
        rules.add(normalJavaRule);

        // 读取groovy文件
//        IRule groovyFile = null;
        List<IRule> groovyFiles = null;
        try {
            groovyFiles = GroovyFactory.INSTANCE.getIRuleFromPackage("groovy");
        } catch (IOException e) {
            LOGGER.error("IOException --> groovy文件读取异常");
            return rules;
        } catch (IllegalAccessException e){
            LOGGER.error("IllegalAccessException --> groovy文件读取异常");
            return rules;
        } catch (InstantiationException e){
            LOGGER.error("InstantiationException --> groovy文件读取异常");
            return rules;
        }

        rules.addAll(groovyFiles);
        return rules;
    }
}
